const fs = require("fs");
const path = require("path");

/**
 * 链表构造
 * @param {*} val
 * @param {*} next
 */
function ListNode(val, next) {
  this.val = val === undefined ? 0 : val;
  this.next = next === undefined ? null : next;
}
/**
 * 数组转链表
 */
const ListToNode = (list) => {
  let node = null;
  for (let i = list.length - 1; i > -1; i--) {
    node = new ListNode(list[i], node);
  }
  return node;
};
/**
 * 链表转数组
 */
const NodeToList = (node) => {
  let arr = [],
    i = 100;
  while (node && i) {
    arr.push(node.val);
    node = node.next;
    i--;
  }
  return arr;
};
/**
 * 调用方法，记录耗时
 */
const timeL = (fun, query) => {
  let time = new Date();
  let resData = fun(...query);
  // if (typeof resData === "object") resData = JSON.stringify(resData);
  let resTime = new Date() - time;
  console.log({ resTime, resData });
  return { resTime, resData };
};

const loadFiles = (directory) => {
  const files = fs.readdirSync(directory);
  files?.forEach((file) => {
    const filePath = path.join(directory, file);
    console.log('filePath', filePath)
    if (path.extname(filePath) === ".js") {
      require(filePath);
    }
  });
};


module.exports = {
  ListNode,
  ListToNode,
  NodeToList,
  timeL,
  loadFiles,
};
